<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/18/018
 * Time: 14:18
 */

namespace app\admin\controller;

use app\common\model\Brand as BrandModel;
use app\common\model\Category as CategoryModel;
use app\common\model\CategoryAndBrand as CategoryAndBrandModel;
use app\common\model\CategoryAndBrand;
use app\common\validate\BrandValidate;
use app\common\validate\IDMustBePostiveInt;
use think\Exception;

class Brand extends Common
{

    /**
     * 获取品牌列表
     * @return \think\response\Json|\think\response\View
     * @throws \think\exception\DbException
     */
    public function index() {
        if ($this->request->isAjax()) {
            $data = BrandModel::getList()->toArray();
            return return_data('品牌列表', 0, $data['data'], $data['total']);
        }
        return view();
    }

    /**
     * 保存品牌
     * @return BrandModel|array|null|\think\response\Json|\think\response\View
     * @throws \think\Exception
     * @throws \think\Exception\DbException
     */
    public function save_brand() {
        $id = input('param.id', 0);
        if (request()->isAjax()) {

            $brand_validate = new BrandValidate();
            $brand_validate->goCheck();
            $param = $brand_validate->getDataByRule(input('param.'));

            $data = BrandModel::createOrUpdateBrand($param, $id);
            return $data;
        } else {
            $data = [];
            if ($id > 0) {
                $data = BrandModel::get($id);
            }
            $this->assign('data', $data);

            return view();
        }
    }

    /**
     * 修改金牌排序
     * @return string|\think\response\Json
     * @throws Exception
     */
    public function save_brand_sort() {
        (new IDMustBePostiveInt())->goCheck();
        $id = input('param.id');
        if ($this->request->isAjax()) {
            $sort = input('param.sort');
            $data = BrandModel::saveSort($id, $sort);
            return $data;
        }
        return 'fail';
    }

    /**
     * 删除品牌
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function delete_brand() {
        (new IDMustBePostiveInt())->goCheck();
        $id = input('param.id');
        $brand = BrandModel::get($id);
        if (!$brand) {
            return return_data('品牌不存在');
        }
        try {
            $brand->delete();
            return return_data('删除成功！');
        } catch (Exception $e) {
            return return_data($e->getMessage(), 1);
        }
    }

    /**
     * 关联分类
     * @return \think\response\Json|\think\response\View
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function relevance_category() {
        (new IDMustBePostiveInt())->goCheck();
        $id = input('param.id');
        if ($this->request->isAjax()) {
            $ids = input('param.ids/a', []);
            $data = CategoryAndBrandModel::brandRelevanceCategory($id, $ids);
            return $data;
        }

        $brand = BrandModel::get($id);
        $this->assign('brand', $brand);

        $brand_category = CategoryAndBrandModel::getIds(['brand_id' => $id], 'category_id');
        $this->assign('brand_category', $brand_category);

        // 获取所有分类
        $categorys = CategoryModel::get_category();
        $this->assign('categorys', $categorys);
        return view();
    }
}